<?php
//$HeadURL: http://instaforms.googlecode.com/svn/trunk/lib/arbrebinaire/instaforms.core.php $
//$Revision: 20 $
//$Date: 2012-08-27 18:55:18 +0000 (Mon, 27 Aug 2012) $
//$Author: dev@arbrebinaire.com $
/**
 *
 * The core class
 *
 * PHP version 5
 *
 * @package    com.arbrebinaire.instaforms
 * @author     Francois Hetu <dev@arbrebinaire.com>
 * @copyright  2012 Francois Hetu
 * @version    SVN: $Id: instaforms.core.php 20 2012-08-27 18:55:18Z dev@arbrebinaire.com $
 */


class InstaFormsCore {

    private function buildResponseString($success, $data){
        $responseArr = array(
            'status' => $success ? 'ok' : 'error',
            'payload' => $success ? $data : 'error'
        );
        return json_encode($responseArr);
    }
    public function prepareFormData($data){
        return $this->buildResponseString($data, $data);
    }
    public function prepareSaveResponse($formId){
        return $this->buildResponseString($formId, $formId);
    }
    public function getFormData($request){
        $formDataObject = new InstaformsFormDataObject($request);
        return $formDataObject;
    }
    public function getRequestedFormId($request){
        return $request['payload']['form_id'];
    }
    public function isSaveRequest($request){
        return $request['req_type'] == 'save';
    }

    public function isLoadRequest($request){
        return $request['req_type'] == 'load';
    }

}

class InstaformsFormDataObject {
    const NEW_FORM      = 'new';
    const ID_FIELD_NAME = 'instaforms-load-form';
    private $formProperties = array();
    private $fields = array();

    public function __construct(array $request){
        $this->fromArray($request);
    }
    public function getId(){
        foreach ($this->formProperties as $property) {
            if($property['name'] == self::ID_FIELD_NAME){
                return $property['value'];
            }
        }
        throw new Exception('Did not find the property ' . self::ID_FIELD_NAME);
    }
    public function toArray(){
        $fields = array();
        foreach($this->fields as $field){
            $fields[] = $field->toArray();
        }
        return array('properties' => $this->formProperties, 'fields_data' => $fields);
    }
    public function fromArray(array $data){
        $payload = $this->getPayload($data);
        $this->formProperties = $payload['properties'];
        if(array_key_exists('fields_data', $payload)){
            foreach($payload['fields_data'] as $field){
                $this->fields[] = new InstaFormsFieldObject($field);
            }
        }
    }
    private function getPayload($data){
        if($data['payload']){
          return $data['payload'];  
        }
        return $data;
    }
    public function getProperties(){
        return $this->formProperties;
    }
    public function getFields(){
        return $this->fields;
    }
    public function isNew(){
        return $this->getId() == self::NEW_FORM;
    }
    public function setId($id){
        foreach ($this->formProperties as $property) {
            if($property['name'] == ID_FIELD_NAME){
                $property['value'] = $id;
            }
        }
    }
}

class InstaFormsFieldObject {
    private $type = '';
    private $id = '';
    private $sentence = '';
    private $validationItems = array();
    private $answers = array();

    public function __construct(array $fieldProperties){
        $this->fromArray($fieldProperties);
    }
    public function toArray(){
        return array('type' => $this->type, 'values' => array('id' => $this->id, sentence => $this->sentence, 'validation' => $this->validationItems, 'answers' => $this->answers));
    }
    public function fromArray(array $data){
        $this->type = $data['type'];
        foreach($data['values'] as $key => $value){
            switch($key) {
                case 'id':
                    $this->id = $value;
                    break;
                case 'sentence':
                    $this->sentence = $value;
                    break;
                case 'validation':
                    $this->validationItems = is_array($value) && count($value) > 0 ? $value : array();
                    break;
                case 'answers':
                    $this->answers = is_array($value) && count($value) > 0 ? $value : array();
                    break;
                default:
                    break;
            }
        }
    }
    public function getType(){
        return $this->type;
    }
    public function getId(){
        return $this->id;
    }
    public function getSentence(){
        return $this->sentence;
    }
    public function getValidationItems(){
        return $this->validationItems;
    }
    public function getAnswers(){
        return $this->answers;
    }
}